﻿/*======================================================================
== Copyright : BlueCurve (c)
== Licence   : Gnu/GPL v2.x
== Author    : Teddy Albina
== Email     : bluecurveteam@gmail.com
== Web site  : http://www.codeplex.com/BlueCurve
========================================================================*/
using System;
using System.AddIn;
using System.Collections.Generic;
using System.IO;
using Common;
using IndexLibrary;
using pdflib;

namespace Addins.Pdf
{
    [AddIn("Addins.Pdf",
    Version = "1.0.0.0",
    Description = "This addin can index PDF documents",
    Publisher = "BlueCurve Project Team")]
    public class AddinsPdf : Plugins.View.AddinsView
    {
        #region 'Configuration'

        /// <summary>
        /// Common functions
        /// </summary>
        private CommonFunctions Common = new CommonFunctions();
        /// <summary>
        /// Eventlog library
        /// </summary>
        private EventLogLibrary eventLog = EventLogLibrary.GetInstance();
        /// <summary>
        /// Propriety dictionnary
        /// </summary>
        private Dictionary<string, string> _file = new Dictionary<string, string>();
        /// <summary>
        /// Entities dictionnary
        /// </summary>
        private Dictionary<string, string> entities = new Dictionary<string, string>();
        /// <summary>
        /// Full Text index library
        /// </summary>
        private FullTextIndexLibrary Index = FullTextIndexLibrary.GetInstance();
        /// <summary>
        /// Serialization class
        /// </summary>
        private Serialization serializ = new Serialization();
        /// <summary>
        /// EntityXML data
        /// </summary>
        private string entityXML = "none";
        /// <summary>
        /// The document text
        /// </summary>
        private string documentText = null;

        #endregion


        #region 'Function'
        
        /// <summary>
        /// The plugin main function
        /// </summary>
        /// <see cref="http://www.codeplex.com/pdflib"/>
        /// <param name="location">file location</param>
        /// <param name="state">file state</param>
        public override void IndexerLoader(string location, int state)
        {
            try
            {
                if (!File.Exists(location))
                    return;

                // Open the file
                ClsPDF Pdfreader = new ClsPDF(location);
                // Document meta datas
                ClsPdfDocumentInformation Pdfinfo = Pdfreader.DocumentInformation;
                
                // Get file informations
                FileInfo info = Pdfreader.FileInfo;

                // Read file content
                StreamReader reader = info.OpenText();
                documentText = reader.ReadToEnd();
                documentText = Common.Stripspaces(documentText);
                reader.Close();

                // Document title
                string documentTitle = Pdfinfo.Title ?? "Untitled document";
                
                // Document description
                string documentDesciption = Pdfinfo.Subject ?? "none";

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                eventLog.WritenewEvent(e.ToString());
            }
        }

        #endregion
    }
}
